package com.chushouya.manager.service.websocket.impl;

import com.chushouya.manager.async.AsyncManager;
import com.chushouya.manager.constants.NoticeUserTypeEnum;
import com.chushouya.manager.dto.admin.im.ClientIdPrefix;
import com.chushouya.manager.dto.admin.im.Content;
import com.chushouya.manager.dto.admin.im.ImBody;
import com.chushouya.manager.dto.admin.im.ImResult;
import com.chushouya.manager.service.async.ManagerAsyncFactory;
import com.chushouya.manager.service.websocket.IMService;
import com.chushouya.manager.service.websocket.IMWebSocketServer;
import com.general.framework.core.lang.Beans;
import com.general.framework.core.lang.Jsons;
import com.general.framework.core.lang.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * IM推送
 */
@Slf4j
@Service
public class IMServiceImpl implements IMService {

    @Override
    @Async
    public ImResult push(ImBody imBody) {
        log.info("===> socket message push: {}", Jsons.toJSONString(imBody));
        ImResult result = new ImResult();
        result.setSuccess(Boolean.TRUE);
        result.setMessage("发送成功");
        if (Lists.isEmpty(imBody.getUserIdList())) {
            result.setSuccess(Boolean.FALSE);
            result.setMessage("发送失败，发送对象为空");
            return result;
        }
        if (Objects.equals(imBody.getUserType(), NoticeUserTypeEnum.adminUser.value())) {
            IMWebSocketServer.broadCast(Beans.copy(imBody, Content.class), ClientIdPrefix.platformAdmin.value(), imBody.getUserIdList());
        } else if (Objects.equals(imBody.getUserType(), NoticeUserTypeEnum.merchant.value())) {
            IMWebSocketServer.broadCast(Beans.copy(imBody, Content.class), ClientIdPrefix.appMerchant.value(), imBody.getUserIdList());
        } else if (Objects.equals(imBody.getUserType(), NoticeUserTypeEnum.store.value())) {
            IMWebSocketServer.broadCast(Beans.copy(imBody, Content.class), ClientIdPrefix.appStore.value(), imBody.getUserIdList());
        } else if (Objects.equals(imBody.getUserType(), NoticeUserTypeEnum.user.value())) {
            IMWebSocketServer.broadCast(Beans.copy(imBody, Content.class), ClientIdPrefix.appUser.value(), imBody.getUserIdList());
        }
        // 如要保存的用户类型
        if(Lists.asList(NoticeUserTypeEnum.adminUser.value()).contains(imBody.getUserType())){
            AsyncManager.me().executeDelay1s(ManagerAsyncFactory.asyncSaveNotice(imBody));
        }
        return result;
    }

    @Override
    public ImResult pushData(ImBody imBody) {
        log.info("===> socket message push: {}", Jsons.toJSONString(imBody));
        if (Objects.equals(imBody.getUserType(), NoticeUserTypeEnum.merchant.value())) {
            IMWebSocketServer.broadCast(Beans.copy(imBody, Content.class), ClientIdPrefix.appMerchant.value(), imBody.getUserIdList());
        } else if (Objects.equals(imBody.getUserType(), NoticeUserTypeEnum.store.value())) {
            IMWebSocketServer.broadCast(Beans.copy(imBody, Content.class), ClientIdPrefix.appStore.value(), imBody.getUserIdList());
        }
        ImResult result = new ImResult();
        result.setSuccess(Boolean.TRUE);
        result.setMessage("发送成功");
        return result;
    }
}
